<?php
    session_start();
    //Konfigurationen laden
    include("config.php");
    
    //Umfrage Klasse laden
    include("umfrage.php");
    
    //Auswertungs Klasse
    include("auswertung.php");
    
    //Error Handling Klasse hinzufügen
    include("Klassen/ErrorHandler.php");
    
    //User KLasse einfügen
    include("Klassen/User.php");
    
    class Control {
        
        protected $auswertung;
        protected $umfrage;
        protected $model;
        protected $view;
        protected $errorH;


        public function __construct() {
            $this->view = new view($this);
            $this->model = new Model($this);
            $this->umfrage = new Umfrage($this);
            $this->auswertung = new Auswertung($this);
            $this->errorH = new ErrorHandler($this);
        }
        
        public function websiteProcess() {
            global $config;
            
            if(isset($_SESSION['page_back'])) {
                if($_SESSION['isloggedin'] == false) {
                    
                    if(filter_input(INPUT_POST, 'submit') == $config['login']) {
                    
                        if(filter_input(INPUT_POST, 'username')) {
                        
                            $this->loginProcess();
                            return;
                         
                        } 
                        
                    }
                    
                    if(filter_input(INPUT_POST, 'submit') == $config['saveuser']) {
                        
                        $this->registrationProcess();
                        return;
                        
                    }
                    
                    $this->view->setLogin();
                    $this->view->setSubmit($config['login']);
                    $this->view->showHTML();
                    
                } 
                
                if($_SESSION['isloggedin'] == true) {
                    echo "Gagu";
                }
                
            } else {
                $this->view->setLogin();
                $this->view->setSubmit($config['login']);
                $_SESSION['page_back'] = true;
                $_SESSION['isloggedin'] = false;
                $this->view->showHTML();
            }
        }
        
        public function registrationProcess() {
            $username = filter_input(INPUT_POST, 'username');
            $passwort = filter_input(INPUT_POST, 'passwort');
            $cryptpasswort = crypt($passwort);
            $passwortwhd = filter_input(INPUT_POST, 'passwortwhd');
            $cryptpasswortwhd = crypt($passwortwhd);
            $vorname = filter_input(INPUT_POST, 'vorname');
            $nachname = filter_input(INPUT_POST, 'nachname');
            $email = filter_input(INPUT_POST, 'email');

            if($passwort == $passwortwhd) {

                $this->model->setBenutzer(new User($username, $cryptpasswort, $vorname, $nachname, $email));
                $this->model->schreibeBenutzer();

                $_SESSION['benutzename'] = $username;
                $_SESSION['vorname'] = $vorname;
                $_SESSION['nachname'] = $nachname;
                $_SESSION['mail'] = $email;
                $_SESSION['isloggedin'] = true;
                
                $this->websiteProcess();

            } else {

                $this->errorH->addErrorMsg('err_passwhd', 'Das Passwort stimmt nicht mit dem ersten &uuml;berein!');
                $this->view->setLogin();
                $this->view->setRegistration(array('err_passwhd'));
                $this->view->setSubmit($config['saveuser']);
                $this->view->setReset($config['resetuser']);
                $this->view->showHTML();
                $this->errorH->removeErrorMsg('err_passwhd');

            }
        }
        
        public function loginProcess() {
            $benutzername = filter_input(INPUT_POST, 'username');
            $password = filter_input(INPUT_POST, 'password');
            $cryptedpasswort = crypt($password);

            $benutzer = $this->model->leseBenutzer($benutzername);

            if($benutzer != null) {
                if($benutzer->getPasswort() == $cryptedpasswort) {
                    $_SESSION['benutzename'] = $benutzer->getUsername();
                    $_SESSION['vorname'] = $benutzer->getVorname();
                    $_SESSION['nachname'] = $benutzer->getNachname();
                    $_SESSION['mail'] = $benutzer->getMail();
                    $_SESSION['isloggedin'] = true;
                    
                    $this->websiteProcess();
                    
                } else {
                    $this->errorH->addErrorMsg("err_passwort", "Leider ist das Passwort zu diesem User falsch.");
                    $this->view->setLogin();
                    $this->view->showHTML();
                }

            } else {
                $this->view->setLogin(array('newuser'));
                $this->view->setRegistration();
                $this->view->setSubmit($config['saveuser']);
                $this->view->setReset($config['resetuser']);
                $this->view->showHTML();
                $this->errorH->removeErrorMsg('newuser');
            }
        }
        
        public function getAuswertung() {
            return $this->auswertung;
        }

        public function getUmfrage() {
            return $this->umfrage;
        }

        public function getModel() {
            return $this->model;
        }

        public function getView() {
            return $this->view;
        }

        public function getErrorH() {
            return $this->errorH;
        }


    }
    
    class view {
        
        protected $outHTML;
        protected $control;
        protected $content;
        
        public function __construct($control) {
            $this->control = $control;
            $this->outHTML = "";
            $this->content = "";
        }
        
        public function showHTML() {
            $this->concatHTML($this->getHeader());
            $this->concatHTML($this->content);
            $this->concatHTML($this->getFooter());
            echo $this->outHTML;
        }
        
        public function concatHTML($stringCode) {
            $this->outHTML .= $stringCode;
        }
        
        public function getHeader() {
            global $config;
            
            return file_get_contents($config['htmlpfad'] . $config['header']);           
        }
        
        public function getFooter() {
            global $config;
            
            return file_get_contents($config['htmlpfad'] . $config['footer']);
        }

        public function setLogin($errorKey = null) {
            global $config;
            
            $this->content .= "<header>"
                    . "<h1>PHPUmfrage</h1>"
                    . "<h2>Ern&auml;hrungsumfrage</h2>"
                    . "</header>";
            
            $this->content .= file_get_contents($config['htmlpfad'] . $config['loginHTML']);
            
            if(isset($errorKey)) {
                $this->fillErrors($errorKey);
            }
            
        }
        
        public function setSubmit($value) {
            $this->content .= '<input type="submit" name="submit" class="submit" value="' . $value . '" id="loginsubmit">';
        }

        public function setReset($value) {
            $this->content .= '<input type="reset" class="reset" value="' . $value . '" id="loginsubmit">';
        }

        public function setRegistration($errorKey = null) {
            global $config;
            
            $this->content .= file_get_contents($config['htmlpfad'] . $config['registration']);
            
            if(isset($errorKey)) {
                $this->fillErrors($errorKey);
            }
        }
        
        public function fillValues() {

        }
        
        public function fillErrors($errorKey) {
            foreach($errorKey as $key) {
                    if(stripos($this->content,'<!--###' . $key . '###-->')) {
                        $this->content = preg_replace('/<!--###' . $key . '###-->/', $this->control->getErrorH()->getErrorMsg($key), $this->content);
                    }
            }
        }
    }
    
    class model {
        
        protected $control;
        protected $benutzer;
        protected $fragen;
        protected $antworten;
        
        public function __construct($control) {
            $this->control = $control;
        }
        
        public function schreibeBenutzer() {
            global $config;

            $einBenutzer = $this->benutzer;
            
            $benutzerstring = $einBenutzer->getUsername() . $config['seperator']
                    . $einBenutzer->getPasswort() . $config['seperator']
                    . $einBenutzer->getVorname() . $config['seperator']
                    . $einBenutzer->getNachname() . $config['seperator']
                    . $einBenutzer->getMail();
            file_put_contents($config['datenpfad'] . $config['benutzer'], $benutzerstring);
            
        }

        public function leseBenutzer($usernametocheck) {
            global $config;
            
            $benutzerdatei = file($config['datenpfad'] . $config['benutzer']);
            for($i = 0; $i < count($benutzerdatei); $i++) { 
                
                $einbenutzer = preg_split('/' . $config['seperator'] . '/', $benutzerdatei[$i]);
                $username = $einbenutzer[0];
                $password = $einbenutzer[1];
                $vorname = $einbenutzer[2];
                $nachname = $einbenutzer[3];
                $mail = $einbenutzer[4];
                
                if($usernametocheck == $username) {
                              
                    $this->benutzer = new User($username, $password, $vorname, $nachname, $mail);  
                    return $this->benutzer;
                    
                }                     
            }
            $this->control->getErrorH()->addErrorMsg('err_newuser', 'Dieser Benutzer existert noch nicht, du kannst dich mit diesem Benutzer registrieren.');
            return 0;
        }
        
        public function setFragen($fragen) {
            $this->fragen = $fragen;
        }     
         
        public function setBenutzer($benutzer) {
            $this->benutzer = $benutzer;
        }
               
        public function getBenutzer() {
            return $this->benutzer;
        }
               
        public function getFragen() {
            return $this->fragen;
        }

        public function getAntworten() {
            return $this->antworten;
        }
        
        public function getControl() {
            return $this->control;
        }
        
    }
    
    //Hier startet der Build der Webseite
    $control = new Control();
    $control->websiteProcess();

?>